import os


class Directory:
    def __init__(self, path):
        """
        自动获取改目录下的文件，没有加判断是不是图片
        :param path:
        """
        folder = os.path.exists(path)
        if not folder:
            # os.makedirs(path)  # makedirs 创建文件时如果路径不存在会创建这个路径
            print("{}路径不存在".format(path))
        else:
            pass
            # print("{}已存在".format(path))
        try:
            self.nameList = os.listdir(path)
            self.path = path
        except:
            print("请正确输入访问目录")

    def get_files_src(self):
        files_src = []
        for i in self.nameList:
            files_src.append("../" + self.path + "/" + i)
        return files_src

    def ret_api(self):
        """
        接收目录文件列表信息，
        :return: 用json格式返回
        inf = {
            "files_src" : ""
            "files": ""
            "path": ""
        }
        """

        inf = {
            "nameList": self.nameList,
            "path": self.path,
            "filesSrc": self.get_files_src()
        }
        return inf
